package cn.SiFuLan.controls;

import cn.SiFuLan.dao.IspuDao;
import cn.SiFuLan.dao.impl.spuDaoImpl;
import cn.SiFuLan.entity.spuEntity;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(urlPatterns = "/admin/update1.shtml")
public class xiugaiServlt1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        spuEntity spu=new spuEntity();

        //1.实例化对象
        SmartUpload smart = new SmartUpload();
        smart.setCharset("utf-8");
        //2.初始化上传
        smart.initialize(this.getServletConfig(),request,response);
        try {
            //3.准备上传
            smart.upload();
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }

        String path = request.getSession().getServletContext().getRealPath("/upload");
        File file = smart.getFiles().getFile(0);
        System.out.println(path);

        System.out.println("获取文件名称："+file.getFileName());
        System.out.println("获取文件大小："+file.getSize());
        System.out.println("获取文件类型："+file.getFileExt());
        System.out.println(path);

        //获取扩展名
        String ext = file.getFileExt();//png   jpg
        //4.保存文件1\\\\\\\\
        //mall/upload/2000202020.png
        String fileName = new Date().getTime()+"."+ext;
        try {
            file.saveAs(path+"/"+fileName);
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }




        String id=smart.getRequest().getParameter("spuid");
        String name=request.getParameter("name");
        String uname=request.getParameter("uname");
        String cid=smart.getRequest().getParameter("cid");
        String bid=smart.getRequest().getParameter("bid");
        String sp=request.getParameter("sp");
        String qd=request.getParameter("qd");
        String fw=request.getParameter("fw");
        String sf=smart.getRequest().getParameter("sf");
        IspuDao ispuDao=new spuDaoImpl();


        spu.setSpu_Name(smart.getRequest().getParameter("name"));
        spu.setTitle(smart.getRequest().getParameter("uname"));
        spu.setSpuimg(fileName);
        if (cid!=null && !cid.equals("")){
            spu.setClass_id(Integer.parseInt(cid));
        }
        if(bid != null && !bid.equals("")){
            spu.setBrand_id(Integer.parseInt(bid));
        }
        spu.setSpu_Info(smart.getRequest().getParameter("sp"));
        spu.setPacking(smart.getRequest().getParameter("qd"));
        spu.setService(smart.getRequest().getParameter("fw"));
        if(sf!=null && !sf.equals("")){
            spu.setTakedown(Integer.parseInt(sf));
        }
        spu.setUpdateTime(new Date());
        if(id!=null && !id.equals("")){
            spu.setSpu_id(Integer.parseInt(id));
        }
        if(ispuDao.update(spu)){
            response.sendRedirect("update.do");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
